//
//  DHBillController.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/16.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHBillController.h"
#import "DHBillCell.h"
#import "DHBillHeaderView.h"
NSString *billCellID = @"billCellID";
@interface DHBillController ()<UITableViewDelegate>
@property(nonatomic ,strong)DHPayLogModel *model;
@property(nonatomic ,assign)int curpage;
@property(nonatomic ,assign)BOOL hasMore;
@property(nonatomic ,strong)NSString *headerTitle;

@end

@implementation DHBillController
- (void)setModel:(DHPayLogModel *)model{
    _model = model;
    
    self.headerTitle = [NSString stringWithFormat:@"账户余额:%@",model.money];
    self.tableView.tableHeaderView = [DHBillHeaderView headerViewWithCount:self.headerTitle];
    [self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self setupNavBar];

}
- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setupUI];
    self.curpage = 1;
    
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark: - 获取数据
- (void)loadDataWithPullup:(BOOL)pullup
{
    if (pullup) {
        if (!self.model) {return;}
        if (self.hasMore) {
            self.curpage++;
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    dictM[@"curpage"] = @(self.curpage);
    dictM[@"is_mouth"] = @(self.is_mouth);
    [DHNetworkManager.shareManager requestPayLogWithParam:dictM completion:^(BOOL isSuccess, DHPayLogModel *model, BOOL hasmore)
    {
        if (!isSuccess) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            return ;
        }
        if (pullup) {
            
            self.model.list = [self.model.list arrayByAddingObjectsFromArray:model.list];
            self.hasMore = hasmore;
            [self.tableView.mj_footer endRefreshing];
        }else{
            self.model = model;
            self.hasMore = hasmore;
            [self.tableView.mj_header endRefreshing];
        }
        
        
    }];
}
#pragma mark: - 设置界面
- (void)setupUI
{

    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.sectionHeaderHeight = 44;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHBillCell" bundle:nil] forCellReuseIdentifier:billCellID];
    self.tableView.backgroundColor = [UIColor colorWithRed:249.0f/255.0f green:249.0f/255.0f blue:249.0f/255.0f alpha:1];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadDataWithPullup:NO];
    }];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadDataWithPullup:YES];
    }];
    self.tableView.mj_header = header;
    self.tableView.mj_footer = footer;
}
#pragma mark: - 设置导航栏
- (void)setupNavBar
{
    self.navigationController.navigationBar.hidden = false;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:222.0f/255.0f green:60.0f/255.0f blue:50.0f/255.0f alpha:1];
    self.title = @"账单明细";
    [self.navigationController.navigationBar setTintColor:UIColor.whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:17],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DHBillCell *cell = [tableView dequeueReusableCellWithIdentifier:billCellID forIndexPath:indexPath];
    cell.model = self.model.list[indexPath.row];
    return cell;
}
@end
